class Booking implements Serializable {
    User user
    Hotel hotel
    Date checkinDate
    Date checkoutDate
    String creditCard
    String creditCardName
    int creditCardExpiryMonth
    int creditCardExpiryYear
    boolean smoking
    int beds

    static constraints = {
        creditCard(blank: false, creditCard: true)
        creditCardName(blank: false, size: 3..70)
        checkinDate(nullable: false, validator: {
            !it.before((new Date()) - 1)
        })
        checkoutDate(nullable: false, validator: {val, obj ->
            !val.before(obj.properties['checkinDate'])
        })
    }

    static mapping = {
        table('T_BOOKING')
    }

    public BigDecimal total() {
        return hotel.price * nights()
    }

    public int nights() {
        return (int) (checkoutDate.getTime() - checkinDate.getTime()) / 1000 / 60 / 60 / 24
    }
}
